\documentclass[12pt,a4paper]{article}

% Margins.
\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\headheight}{12pt}
\setlength{\headsep}{42pt}
\setlength{\topmargin}{-54pt}
\setlength{\textwidth}{6.5in}
\setlength{\textheight}{10in}

\usepackage{amsmath}
\usepackage{float}
\usepackage{graphicx}
\usepackage[hyphens]{url}
\usepackage{hyperref}	% Clickable links to figures, references and urls.
\usepackage{datetime}
\usepackage{longtable}

% Links direct to top of figures.
\usepackage[all]{hypcap}

% Drawing.
\usepackage{pgf}
\usepackage{tikz}

% Listings for formatting code.
\usepackage{listings}
\usepackage{textcomp}
% General options.
\lstset{breaklines=true, basicstyle=\small\ttfamily, tabsize=4, numbers=left, stepnumber=1, frame=single, showstringspaces=false, upquote=true}
% C++ specific high-lighting. Comments are 50/50 shades of green/black and strings coloured with 60/40 red/black mixture.
\lstset{language=[ISO]C++, commentstyle=\color{green!50!black}, keywordstyle=\color{blue}, stringstyle=\color{red!60!black}}

%opening
\title{\vspace{-2cm}A Guide to Network Programming Using C++}
\author{Attique Dawood}
\date{September 01, 2014\\[0.2cm] Last Modified: \today, \currenttime}
\begin{document}
\maketitle
\section{Introduction}
Computer networks are widespread nowadays and internet is the largest network of computers today. As such communication between computers is an essential part of any computer network. There are computer standards and protocols that define how computers talk to each other. The Sockets API (application programming interface) is a set of libraries implemented for different computer languages that can be used to develop network applications.

This guide comes with C++ wrapper classes for the C Sockets API that make it easier to quickly get started with network programming. It is intended for novice level C++ programmers and students who want to jump right into network programming without worrying about minute details\footnote{For an in-depth guide refer to Beej's Guide to Network Programming \cite{beej-ref}}.

The C++ wrapper classes are portable on windows and linux. This essentially means the programmer can write the same piece of code using wrapper classes and it will compile and run on both windows and linux. Even cross--OS communication is also possible if sender/receiver are compiled and run on different OSs.
\section{Network Programming Concepts}
\subsection{IP Address}
IP Address is the address of a computer. To send a message to a particular computer we need its IP address.
\subsection{Port}
Let's say Person A and Person B live in a House. If you want to send a letter to Person A you'll first write the house address and on top of it mention that it is addressed to Person A. Similarly, in a computer, hundreds of processes may be running at a time. To send a message to a particular process you mention \textbf{Port} number of process. Two processes cannot have the same port number. So, processes in a computer are distinguished by their port numbers. A port number can be any number in the range 1--65535. Usually port numbers 1--200 are reserved for system processes, e.g., port 80 is for http protocol. Rest of the port numbers can be acquired by any process.
\subsection{Socket}
Socket is the mail box outside your home. You put letter in it that are taken away by postman and you can take take letters out of it that postman delivers. A process must associate its port number and IP address with a socket in order to communicate with outside world.
\subsection{\texttt{bind()}}
\verb|bind()| is used to bind IP address, port number and socket together. This is necessary before communication can start.
\subsection{\texttt{send()} and \texttt{receive}}
\verb|send()| or \verb|sendto()| sends data or message, whereas, \verb|receive()| or \verb|receivefrom()| receives data. \verb|receive|'ing is blocking. A process will wait indefinitely until it receives a message. If two processes are communicating then they must NOT call \verb|receive()| at the same time or they will both block. \verb|send()| and \verb|receive()| calls must alternate between two processes so that if one process is \verb|send|'ing the other is \verb|receive|'ing. It is normally a good practice to ensure that consecutive \verb|send()| or \verb|receive()| calls are never made by a single process. But this isn't necessary if process knows beforehand exactly how many bytes it needs to send or receive.
\subsection{TCP and UDP}
TCP and UDP are two modes of communication in IP.
\begin{itemize}
\item TCP uses \verb|send()| and \verb|receive()| while UDP uses \verb|sendto()| and \verb|receivefrom()|.
\item UDP is like posting a letter. Any packet can be sent to or received from any IP address. 
\item TCP is like a phone call. A connection is established between a Server and Client. Client is the one calling and Server is the one accepting calls.
\item Although Client and Server don't exist in UDP technically, the two computers communicating using UDP are commonly distinguished by calling one Server and the other Client. Normally Server is the one that starts in waiting (\verb|receivefrom()|) state.
\end{itemize}
\section{TCP Model}
\subsection{Server}
\begin{itemize}
\item 2 sockets, 1 for accepting connections and another for communication with  Client.
\item Maintains at least two address structures, one for itself and one to store Client information. 
\item There can be multiple Clients connected to Server.
\item Sequence of Operation:
	\begin{enumerate}
    \item Create Server \verb|socket()|.
    \item \verb|bind()| Server socket with its address and port.
    \item Set Server socket options, \verb|setsockopt()| (optional).
    \item Configure Server socket to \verb|listen()| to any incoming connection requests.
    \item Set Server socket state to \verb|accept()| any connection request. This is blocking. When a connection is accepted it will return Client socket and Client address is stored.
    \item Use Client socket to \verb|send()| and \verb|recv()| data to that particular Client.
    \item \verb|close()| sockets before exiting.
	\end{enumerate}
\end{itemize}
\subsection{Client}
\begin{itemize}
\item 1 socket to connect and \verb|send()| and \verb|recv()| data.
\item Maintains two address structures, for Client and Server.
\item Sequence of Operation:
	\begin{enumerate}
    \item Create Client \verb|socket()|.
    \item \verb|bind()| Client socket with its adress and port.
    \item \verb|connect()| to a Server using Server Address and Server Port.
    \item \verb|send()| and \verb|recv()| using Client socket.
    \item \verb|close()| Client socket before exiting.
	\end{enumerate}
\end{itemize}
\section{UDP Model}
\begin{itemize}
\item Server does not need to \verb|listen()| or \verb|accept()| connections.
\item Client does not need to \verb|connect()|.
\item Server and Client are mirrors. Only logical difference is sequence of \verb|sendto()| and \verb|recvfrom()| calls.
\item Sequence of Operation:
	\begin{enumerate}
    \item Create \verb|socket()|.
    \item \verb|bind()| socket to own address and port.
    \item \verb|sendto()| `their' address or \verb|recvfrom()| `random' address on own socket.
    \item \verb|close()| socket.
    \end{enumerate}
\end{itemize}
\section{Using the Provided Code}
The code provided has two examples, one for each TCP and UDP communication model. Extract the zip files and open the solution with the \verb|.sln| file if using Visual Studio 2010 on windows. Each solution contains two projects for Client and Server. On linux use the provided \verb|Makefile| to compile and run. From the root folder of solution either use \verb|make runS| or \verb|make runC| to run server or client, respectively. You should make changes to \verb|ClientMain.cpp| and \verb|ServerMain.cpp| files.

If \verb|winsock2.h| cannot be included from TCP/UDP examples then install the Microsoft SDK. Here's the link to Microsoft SDK  for Windows 7: \url{http://www.microsoft.com/en-us/download/details.aspx?id=8279}
\section{Useful Functions}
\subsection{Send/Receive Variables}
Variables can by anything \verb|int|, \verb|float|, \verb|char| or even whole \verb|struct| type variables.
\begin{lstlisting}
// Send or receive a variable. Obj can be server or client object/variable. 
int x = 3;
char c = '$';
struct s
{
	int x;
	float f;
};
s structvar;
// Sending functions. Use SendTo for UDP.
Obj.Send((void*)&x, sizeof(int));
Obj.Send((void*)&c, sizeof(char));
Obj.Send((void*)&structvar, sizeof(s));

int xtemp;
char ctemp;
s structtemp;
// Receiving functions. Use ReceiveFrom for UDP.
Obj.Receive((void*)&xtemp, sizeof(int));
Obj.Receive((void*)&ctemp, sizeof(char));
Obj.Receive((void*)&structtemp, sizeof(s));
\end{lstlisting}
\subsection{Send/Receive Whole Arrays}
\begin{lstlisting}
const int SIZE = 20;
int Data[SIZE];
Obj.Send((void*)Data, sizeof(Data)); // sizeof(Data) = sizeof(int)*SIZE

int RecData[SIZE];
Obj.Receive((void*)RecData, sizeof(Data));
\end{lstlisting}
\subsection{Send/Receive Strings}
Strings can be sent in a similar manner, i.e., by sending whole arrays. However, note that length of a string is normally variable and we wouldn't want to send any extra characters after NULL. If sender sends NULL character along with string then receiver doesn't need to do any additional processing. If sender doesn't send NULL then receiver has to append a NULL to received string.

A convenient \verb|receive()| function is available that automatically appends NULL to received string and stores the received string in a built--in buffer. Maximum size of internal buffer is 512 bytes so maximum string length can be 511. Larger strings must be split and sent separately.

The essential point is that sender and receiver must agree on a communication scheme. That is whether whole arrays are to be sent, sender appends NULL or not, maximum size of buffers etc.
\begin{lstlisting}
char Message[30] = "Hello world!";
Obj.Send((void*)Message, strlen(Message)+1); // Send string + NULL character.
// Can also send const strings.
Obj.Send((void*)"Hello", strlen("Hello")+1);

// When receiving a string we have the option to use the built-in Receive()/ReceiveFrom() functions without arguments. These automatically append a NULL to received string and stores in internal buffer.
Obj.Receive(); // or Obj.ReceiveFrom(); for UDP.
cout << Obj.GetBuffer() << endl; // Display received string from internal buffer.
\end{lstlisting}
\section{Keeping Track of Data Sent/Received}
The send and receive functions return the number of bytes that were actually sent and received. Receiver may not always know exactly how many bytes it is going to receive beforehand. This can be illustrated using example given below. Sender sends a string to receiver. Both agree on a maximum array size of 512. The length of string can be anything between 1 and 511.
\begin{lstlisting}
// Send whole string including NULL.
char Message[512] = "Hello world!";
int BytesSent; //  BytesSent should be equal to sizeof(Message)+1.
BytesSent = Obj.Send((void*)Message, strlen(Message)+1); // Send string + NULL character.

char Buffer[512];
int ByteseReceived;
BytesReceived = Obj.Receive((void*)Buffer, 512); // 512 is max buffer size. Received bytes will be equal to sent bytes.
\end{lstlisting}
%\nocite{*}
\bibliographystyle{plain}
\bibliography{NetworkRef}
\end{document}
